<?php 
/**
  * CLOUD
  *
  * This is not a freeware, use is subject to license terms
  * @author CLOUD DEV TEAM
  * 
  */
if (!defined('IN_CLOUD')) exit('Access Denied!');

class Image 
{
	
	private $s_img;
	private $s_info;
	private $s_w;
	private $s_h;
	private $new_img;
	private $name;
	
	/**
	 * 水印文件
	 * @var string
	 */
	private $w_img;
	
	/**
	 * 清晰度
	 * @var int
	 */
	private $w_pct;
	
	/**
	 * jpeg图片质量
	 * @var int
	 */
	private $jpeg_quality;
	
	/**
	 * 水印类型
	 * @var tring
	 */
	private $w_type;
	
	/**
	 * 图片最小宽度
	 * @var int
	 */
	private $w_min_w;
	
	/**
	 * 图片最小高度
	 * @var int
	 */
	private $w_min_h;
	
	/**
	 * 文字水印
	 * @var string
	 */
	private $w_text;
	
	/**
	 * 文字大小
	 * @var int
	 */
	private $w_text_size;
	
	/**
	 * 文字颜色
	 * @var string
	 */
	private $w_text_color;
	
	/**
	 * 水印位置
	 * @var int
	 */
	private $w_pos;
	
	/**
	 * 根目录
	 * @var string
	 */
	private $rootdir;
	
	/**
	 * 图片类型
	 * @var int
	 */
	private $img_type;
	
	public function __construct()
	{
		$systemConfig = Common::getConfig( 'System' );
		$this->w_type = ( int )$systemConfig['w_type'];
		if ( $systemConfig['w_img_type'] == 0 )
		{
			$this->w_img = STATICS . 'Image/Watermark/watermark.png';
		}
		else
		{
			$this->w_img = STATICS . 'Image/Watermark/watermark.gif';
		}
		$this->w_pct = $systemConfig['w_pct'] ? $systemConfig['w_pct'] : 70;
		$this->jpeg_quality = $systemConfig['w_jpeg_quality'] ? $systemConfig['w_jpeg_quality'] : 80;
		$this->w_min_w = ( int )$systemConfig['w_min_w'];
		$this->w_min_h = ( int )$systemConfig['w_min_h'];
		$this->w_text = $systemConfig['w_text'] ? $systemConfig : 'www.bluecms.net';
		$this->w_text_size = $systemConfig['w_text_size'] ? $systemConfig['w_text_size'] : 20;
		$this->w_text_color = $systemConfig['w_text_color'] ? $systemConfig['w_text_color'] : '#000000';
		$this->w_pos = $systemConfig['w_pos'] ? $systemConfig['w_pos'] : 9;
		
	}
	
	/**
	 * 图片处理前的初始操作
	 * @param string $img
	 */
	private function init( $img )
	{
		$this->rootdir = ROOT_DIR . ATTACHMENT;
	    $this->s_img = $img;
        $this->s_info = getimagesize( $this->s_img );
        if ( !$this->s_info )
        {
            return false;
        }
        $this->s_w = $this->s_info[0];
        $this->s_h = $this->s_info[1];
        $this->new_img = $this->create_img_resource( $this->s_info[2] , $this->s_img );
        if ( !$this->new_img )
        {
            return false;
        }
	}
	
	/**
	 * 生成缩略图
	 * @param string $img
	 * @param int $width
	 * @param int $height
	 * @param int $is_del	是否删除大图
	 */
	public function smallImg( $img , $width , $height , $is_del = false )
	{
		$this->init( $img );
		$name = basename( $img );
		$dir = substr( $img , 0 , strrpos( $img , '/' ) ) . '/';
		$newdir = str_replace( $this->rootdir , '' , $dir );
		$new_name = 'thumb_' . $name;
		
		if( $this->s_info[2] == 1 ) 
		{
            $im = imagecreatefromgif($img);
        } 
        elseif( $this->s_info[2] == 2 ) 
        {
            $im = imagecreatefromjpeg( $img );
        } 
        elseif( $this->s_info[2] == 3 ) 
        {
            $im = imagecreatefrompng($img);
        } 
        else 
        {
            $im = '';
        }
        if( empty( $im ) ) return false;
        
        //宽高
        $width = ($width > $this->s_w) ? $this->s_w : $width;
        $height = ($height > $this->s_h) ? $this->s_h : $height;

        if ( function_exists( "imagecreatetruecolor" ) ) 
        {
            $new_img = imagecreatetruecolor( $width , $height );
            imagecopyresampled( $new_img , $im , 0 , 0 , 0 , 0 , $width , $height , $this->s_w , $this->s_h );
        } 
        else 
        {
            $new_img = imagecreate($width, $height);
            imagecopyresized( $new_img , $im , 0 , 0 , 0 , 0 , $width , $height , $this->s_w , $this->s_h );
        }

        if ( file_exists( $dir . $new_name ) ) 
        {
        	@unlink( $dir . $new_name );
        }
        
		switch( $this->s_info[2] ) 
		{
	        case 1: imagegif( $new_img , $dir . $new_name ); break;
	        case 3: imagepng( $new_img , $dir . $new_name ); break;
	        default: imagejpeg( $new_img , $dir . $new_name , $this->jpeg_quality );
	    }
        imagedestroy( $new_img );
        imagedestroy( $im );
        if ( $is_del === true )
        {
            @unlink( $img );
        }
        return $newdir . $new_name;
	}
	
	/**
	 * 给图片添加水印
	 * @param string $img
	 */
	public function watermark( $img )
	{
		$this->init( $img );
		if ( $this->w_type == 1 )
		{
			if ( $this->s_w < $this->w_min_w || $this->s_h < $this->w_min_h )
			{
				return false;
			}
			$water_img_info = getimagesize( $this->w_img );
			$w_img = $this->create_img_resource( $water_img_info[2] , $this->w_img );
			$w_w = $water_img_info[0];
			$w_h = $water_img_info[1];
			$w_pos = $this->get_pos( 'img' , $w_w , $w_h );
			$x = $w_pos['x'];
			$y = $w_pos['y'];
			if ( $water_img_info[2] == 1 )
			{
				imagecopymerge( $this->new_img , $w_img , $x , $y , 0 , 0 , $w_w , $w_h , $this->w_pct );
			}
			else
			{
				@imagealphablending( $this->new_img , true );
				@imagecopy( $this->new_img , $w_img , $x , $y , 0 , 0 , $w_w , $w_h );
			}
			
			switch( $this->s_info[2] ) 
			{
		        case 1: imagegif( $this->new_img , $this->s_img ); break; 
		        case 3: imagepng( $this->new_img , $this->s_img ); break;
		        default: imagejpeg( $this->new_img , $this->s_img , $this->jpeg_quality ); break;
		    }
		    imagedestroy( $this->new_img );
		}
	}
	
	/**
	 * 获取水印位置
	 * @param string $type	水印类型（文字|图片）
	 * @param int $width
	 * @param int $height
	 */
	private function get_pos( $type , $width = '' , $height = '' )
	{
		if ( $type == 'text' )
		{
			$text_arr = imagettfbbox( $this->w_text_size , 0 , $this->w_text_font , $this->w_text );
			$width = $text_arr[2] - $text_arr[6];
			$height = $text_arr[3] - $text_arr[7];
		}
		if ( $this->s_w < $width || $this->s_h < $height )
		{
			return false;
		}
		switch ( $this->w_pos )
		{
			case 1:
				$x = 5;
				$y = 5;
				break;
				
			case 2:
				$x = ( $this->s_w - $width ) / 2;
				$y = 5;
				break;
				
			case 3:
				$x = $this->s_w - $width;
				$y = 5;
				break;
				
			case 4:
				$x = 5;
				$y = ( $this->s_h - $height ) / 2;
				break;
				
			case 5:
				$x = ( $this->s_w - $width ) / 2;
				$y = ( $this->s_h - $height ) / 2;
				break;
				
			case 6:
				$x = $this->s_w - $width -5;
				$y = ( $this->s_h - $height ) / 2;
				break;
				
			case 7:
				$x = 5;
				$y = $this->s_h - $height - 5;
				break;
				
			case 8:
				$x = ( $this->s_w - $width ) / 2;
				$y = $this->s_h - $height -5;
				break;
				
			case 9:
				$x = $this->s_w - $width -5;
				$y = $this->s_h - $height -5;
				break;
				
			default:
				$x = rand( 0, $this->s_w - $width );
				$y = rand( 0, $this->s_h - $height );
				break;
		}
		return array( 'x' => $x, 'y' => $y );
	}
	
	/**
	 * 新建图像
	 * @param int $type			图片类型
	 * @param string $image
	 */
	function create_img_resource( $type , $image )
	{
		if ( $type == 1 )
		{
			return imagecreatefromgif( $image );
		}
		elseif ( $type == 2 )
		{
			return imagecreatefromjpeg( $image );
		}
		elseif ( $type ==3 )
		{
			return imagecreatefrompng( $image );
		}
		return false;
	}
	
	/**
	 * 获取图片信息
	 * @param string $img	图片路径
	 */
    public function get_info( $img ) 
    {
        $image = $this->rootdir . $img;
        $image_info = getimagesize( $image );
        $image_info['width'] = $image_info[0];
        $image_info['height'] = $image_info[1];
        $image_info['type'] = $image_info[2];
        $image_info['name'] = basename( $image );
        $image_info['dir'] = substr( $img , 0 , strrpos( $img , '/' ) ) . '/';
        return $image_info;
    }
    
    /**
     * 根据大图路径，获取缩略图路径
     * @param unknown_type $path
     */
    public static function getThumbPath( $path )
    {
    	if ( empty( $path ) )
    	{
    		return false;
    	}
    	return dirname( $path ) . '/thumb_' . basename( $path );
    }
    
    public static function getBigImagePath( $thumbPath )
    {
    	return dirname( $thumbPath ) . '/' . str_replace( 'thumb_' , '', basename( $thumbPath ) );
    }
}